package com.cango.framework.base.job;

import com.cango.qutz.web.entity.ScheduleJob;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by weih on 2016/10/21.
 */
public class MailTemplateUtils {
    private static Log log = LogFactory.getLog(MailTemplateUtils.class);
    private static VelocityEngine ve = null ;
    private static  String TITLE_TEMPLATE = "";
    private static  String CONTENT_TEMPLATE = "";
    private static String envName= "profiles/prd";

    public MailTemplateUtils(String name){
        envName=name;
    }
    static{
        Properties p = new Properties();
        String path = Thread.currentThread().getContextClassLoader().getResource("application.properties").getPath();
        String rootPath = Thread.currentThread().getContextClassLoader().getResource("conf/template").getPath();
        TITLE_TEMPLATE = "joberror_email_title.vm";
        CONTENT_TEMPLATE = "joberror_email_content.vm";
        try {
            p.load(new FileInputStream(new File(path)));
            p.put(Velocity.FILE_RESOURCE_LOADER_PATH,rootPath);
        } catch (IOException e) {
            log.error("模板文件不存在",e);
        }
        //初始化并取得Velocity引擎
        ve = new VelocityEngine();
        ve.init(p);
    }

    public static String getTtile(ScheduleJob job){
        //取得velocity的上下文context
        VelocityContext context = new VelocityContext();
        context.put("jobId",job.getJobId());
        context.put("scheduleId",job.getScheduleId());
        context.put("jobGroup",job.getJobGroup());
        context.put("jobName",job.getJobName());
        context.put("methodName",job.getMethodName());

        context.put("envName",envName);
        // 输出流
        StringWriter writer = new StringWriter();
        // 转换输出
        ve.mergeTemplate(TITLE_TEMPLATE, "UTF-8", context,writer);
        return writer.toString();
    }
    public static String applyContent(Map<String, Object> param){
        //取得velocity的上下文context
        VelocityContext context = new VelocityContext();
        //把数据填入上下文
        if(null != param && !param.isEmpty()){
            for(String key : param.keySet()){
                context.put(key,param.get(key));
            }
        }
        // 输出流
        StringWriter writer = new StringWriter();
        // 转换输出
        ve.mergeTemplate(CONTENT_TEMPLATE, "UTF-8", context,writer);
        return writer.toString();
    }

    public static void main(String[] args){
        ScheduleJob job = new ScheduleJob();
        job.setScheduleId(1);
        job.setJobName("测试");
        job.setJobGroup("测试");
        job.setMethodName("getAllCarBrand2VOList");
        String s = Thread.currentThread().getContextClassLoader().getResource("conf/template").getPath();
//        System.out.println(getTtile(job));

        Map<String, Object> contentParam = new HashMap<String, Object>();
        try {
            contentParam.put("jobId", job.getJobId());
            contentParam.put("scheduleId", job.getScheduleId());
            contentParam.put("jobGroup", job.getJobGroup());
            contentParam.put("jobName", job.getJobName());
            contentParam.put("methodName", job.getMethodName());
            contentParam.put("param", "{}");

            Date beginDate = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String nowTime = format.format(beginDate);
            contentParam.put("executeTime", nowTime);
            int i=5/0;
        }catch (Exception e){
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            contentParam.put("error",sw.toString());
            System.out.println(applyContent(contentParam));
        }

    }

}
